package com.upcode.weighing.detect.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.domain.BaseParam;
import com.upcode.weighing.log.OpsLog;
import com.upcode.weighing.service.BaseParamService;
import com.upcode.weighing.vo.req.BaseParamAddVo;
import com.upcode.weighing.vo.resp.BaseResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author alex
 * @since 2024-07-21
 */
@Controller
@RequestMapping("/baseparam")
@Api(value = "/baseparam",description = "基础参数接口",produces = MediaType.ALL_VALUE,tags = {"基础参数接口"})
public class BaseParamController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseParamController.class);
    @Autowired
    private BaseParamService baseParamService;
    /**
     * 新增系统账号
     */
    @OpsLog(operation = "基础参数接口/添加集成参数" ,type = 2)
    @PostMapping("/add")
    @ApiOperation(value = "添加基础参数", notes = "添加", httpMethod = "POST")
    public Object add(@Validated @RequestBody BaseParamAddVo baseParamAddVo) {
        BaseParam baseParam=new BaseParam();
        BeanUtils.copyProperties(baseParamAddVo,baseParam);
        baseParam.setId(IdWorker.getId());
        try{
            baseParamService.saveOrUpdate(baseParam);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return error(e.getMessage());
        }

        return success("添加检测记录成功");
    }
  /*  *//**
     * 获取检测记录分页列表
     *//*
    @OpsLog(operation = "检测点接口/分页查询" ,type = 1)
    @PostMapping("/queryPage")
    @ApiOperation(value = "检测点分页查询",notes = "")
    public ResponseEntity<BaseResponseVo<IPage<DetectPoint>>> queryPage(@RequestBody PageReqVo<DetectPointQueryParam> queryParamPageReqVo){
        IPage<DetectPoint> page = detectPointService.queryPageList(queryParamPageReqVo);
        return  HttpResponse.success(page);
    }*/

    /**
     * 获取检测点列表
     */
    @OpsLog(operation = "基础参数接口/列表查询" ,type = 1)
    @PostMapping("/queryList")
    @ApiOperation(value = "基础参数查询",notes = "")
    public  ResponseEntity<BaseResponseVo<List<BaseParam>>> queryList(){
        List<BaseParam> baseParamList = baseParamService.list();
        return HttpResponse.success(baseParamList);
    }

    /**
     * 根据id修改信息
     */
    @OpsLog(operation = "基础参数接口/更新基础参数" ,type = 4)
    @PostMapping("/update")
    @ApiOperation(value = "更新基础参数" ,notes = "")
    public Object updateById(@Validated @RequestBody BaseParam baseParam){
        try {
            if(!baseParamService.updateById(baseParam)){
                return error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return success("修改成功");
    }

    /**
     * 根据id修改删除
     */
    @OpsLog(operation = "删除" ,type = 4)
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "基础参数删除" ,notes = "")
    public Object delete(@PathVariable("id")@NotNull(message = "id 不能为空") Long id){
        try {
            if(!baseParamService.removeById(id)){
                return error("删除失败");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return error(e.getMessage());
        }
        return success("删除成功");
    }

}
